<?php
/**
 * Created by JetBrains PhpStorm.
 * User: FangXuanLing
 * Date: 13-11-19
 * Time: 下午1:38
 * To change this template use File | Settings | File Templates.
 */
class CategoryController extends Controller{

    public function actionIndex(){
        //第一个参数是模板的名字
        //第二个参数是要传递给模板的变量
        /*在模板里可以直接这样使用 <?php echo $name ?>*/
        $model = Category::model()->findAll();
        $this->render('index',array("name"=>"分类列表","data"=>$model));
    }

    public function actionAdd(){
        $category_name = Yii::app()->request->getParam('category_name');
        if(Yii::app()->request->isPostRequest){
            $category = new Category();
            $category->category_name = $category_name;
            $category->save();
            $this->redirect(array('admin/category/add'));
        }
        else{
            $this->render('add');
        }

    }

    public function actionEdit(){
        $cid = Yii::app()->request->getParam('cid');
        //是post请求且cid不为空
        if(Yii::app()->request->isPostRequest){
            $model = Category::model()->findByPk($cid);
            $model->category_name = Yii::app()->request->getParam('category_name');
            $model->save();
            $this->redirect(array('admin/category/index'));
            //Yii::app()->user->putSuccessMsg('操作成功啦！');
        }
        else{
            $model = Category::model()->find("cid=:cid",array(":cid"=>$cid));
            $this->render('edit',array('model'=>$model));
            //Yii::app()->user->putErrorMsg('数据不存在！');
        }

    }

    public function actionDel(){
        $cid = Yii::app()->request->getParam('cid');
        if(empty($cid)){
            Yii::app()->user->putErrorMsg('数据不存在！');
            //Yii::app()->user->putNoticeMsg('操作警告...');
        }
        else{
            $model = Category::model()->findByPk($cid);
            $model->delete();
            $this->redirect(array('admin/category/index'));
            //Yii::app()->user->putSuccessMsg('操作成功啦！');
        }
    }
}